package com.proton.carepatchtemp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapterholder.NoteHolder;
import com.proton.carepatchtemp.database.table.Note;
import com.proton.carepatchtemp.utils.FormatUtils;
import com.proton.carepatchtemp.utils.net.ReportCenter;

import java.util.List;

/**
 * Created by MoonlightSW on 2016/12/14.
 */

public class NotesAdapter extends RecyclerView.Adapter<NoteHolder> {

    private Context mContext;
    private List<Note> mNotes;
    private LayoutInflater mInflater;

    public NotesAdapter(Context context, List<Note> notes) {
        this.mContext = context;
        this.mNotes = notes;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_note, parent, false);
        return new NoteHolder(view);
    }

    @Override
    public void onBindViewHolder(NoteHolder holder, int position) {
        Note temp = mNotes.get(position);
        if (temp == null) return;

        switch (temp.getType()) {
            case 1:
                holder.mIv_note_type.setImageResource(R.mipmap.notes_drug_treatment_img);
                break;
            case 2:
                holder.mIv_note_type.setImageResource(R.mipmap.notes_physical_therapy_img);
                break;
            case 3:
                holder.mIv_note_type.setImageResource(R.mipmap.notes_physical_condition_img);
                break;
            case 7:
                holder.mIv_note_type.setImageResource(R.mipmap.notes_comment_img);
                break;
            default:
                break;
        }

        holder.mTv_note_time.setText(FormatUtils.transferLongToDate("yyyy" + mContext.getString(R.string.string_year) + "MM" + mContext.getString(R.string.string_month) + "dd" + mContext.getString(R.string.string_month) + "HH:mm:ss", temp.getCreated()));

        holder.mTv_note_content.setText(temp.getContent());

        holder.mIv_delete_note.setOnClickListener(view -> {
            holder.mSwipe_refresh_layout.quickClose();
            ReportCenter.deletecomment(temp.getId());
            mNotes.remove(position);
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return null == mNotes ? 0 : mNotes.size();
    }
}
